/*
 * @Author: heinan
 * @Date: 2023-07-23 22:50:43
 * @Last Modified by: heinan
 * @Last Modified time: 2023-08-01 21:54:28
 */
"use strict";
const { Controller } = require("egg");

/**
 *@Controller 身份管理
 */
class IdentityController extends Controller {
  /**
   * @Summary 用户身份列表
   * @Description 获取用户身份列表
   * @Router GET /api/v1/identity
   */
  async index() {
    const result = await this.ctx.service.identity.index();
    if (result.length) {
      this.ctx.body = {
        code: 1,
        message: "查询成功!",
        data: result,
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "暂无数据!",
        data: [],
      };
    }
  }
  /**
   * @Summary 创建身份
   * @Description 创建用户身份
   * @Router POST /api/v1/identity
   * @Request body createIdentity *body
   */
  async create() {
    const result = await this.ctx.service.identity.create(
      this.ctx.request.body
    );
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "新增成功!",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "新增失败!",
      };
    }
  }
  /**
   * @Summary 更新身份
   * @Description 更新用户身份
   * @Router PUT /api/v1/identity
   * @Request body updateIdentity *body
   */
  async update() {
    const result = await this.ctx.service.identity.update({
      ...this.ctx.params,
      ...this.ctx.request.body,
    });
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "修改成功!",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "修改失败!",
      };
    }
  }
  /**
   * @Summary 删除身份
   * @Description 删除用户身份
   * @Router DELETE /api/v1/identity/{id}
   * @Request path string *id
   */
  async destroy() {
    const result = await this.ctx.service.identity.destroy(this.ctx.params);
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "删除成功!",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "删除失败!",
      };
    }
  }
  /**
   * @Summary 获取用户身份
   * @Description 根据用户id获取用户身份
   * @Router GET /user/identity/${id}
   * @Request path string *id
   */
  async getIdentityById() {
    const result = await this.ctx.service.identity.getIdentityById(
      this.ctx.params
    );
    if (result.length) {
      this.ctx.body = {
        code: 1,
        message: "查询成功!",
        data: result,
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "暂无数据!",
        data: [],
      };
    }
  }
}

module.exports = IdentityController;
